package com.yituo.mjworld.core.web.core;


import com.alibaba.fastjson.JSONObject;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import java.io.IOException;

/**
 * @author Administrator
 */
public class MyHttpResponse extends HttpServletResponseWrapper {
  /**
   * 后端编写的类,编码错误.用于快速定位编码错误.
   */
  public static final int RES_STATUS_301_CODE_ERROR = 301;
  /**
   * 前端传递的参数,错误
   */
  public static final int RES_STATUS_302_PARAM_ERROR = 302;
  /**
   * Token 校验不通过
   */
  public static final int RES_STATUS_401_TOKEN_ERROR = 401;
  /**
   * serlvet 接到请求,执行出错
   * 比如:
   * 无匹配方法.
   * 方法不可访问
   * 方法内部执行出错
   */
  public static final int RES_STATUS_404_UNKNOW_ERROR = 404;
  /**
   * 成功
   */
  public static final int RES_STATUS_200_SUCCESS = 200;
  /**
   * URL错误!
   */
  public static final int RES_STATUS_500_URL_ERROR = 500;

  private static final int CODE_SUCCESS = 200;
  private static final int CODE_FAILED = 500;
  private static final String MSG_SUCCESS = "响应成功!";
  private static final String MSG_FAILED = "响应失敗!";
  private JSONObject result;
  private Result processResult;

  /**
   * @param response
   */
  public MyHttpResponse(HttpServletResponse response) {
    super(response);
    this.setContentType("text/javascript;charset=utf-8");
    this.setCharacterEncoding("utf-8");
    result = new JSONObject();
  }

  /**
   * 响应成功
   *
   * @param data 需要返回的数据,需要根据实际业务指定,因此这里只能给定data对象
   * @return
   */
  public void success(Object data) throws IOException {
    result.put("code", CODE_SUCCESS);
    if (null != data) {
      result.put("data", data);
    } else {
      result.put("data", MSG_SUCCESS);
    }
    this.setStatus(RES_STATUS_200_SUCCESS);
    this.getWriter().append(result.toString());
  }

  /**
   * 响应失败
   *
   * @param data 需要返回的数据,需要根据实际业务指定,因此这里只能给定data对象
   * @return
   */
  public void failedFor500(Object data) throws IOException {
    this.setStatus(RES_STATUS_500_URL_ERROR);
    result.put("code", CODE_FAILED);
    if (null != data) {
      result.put("data", data);
    } else {
      result.put("data", MSG_FAILED);
    }
    this.getWriter().append(result.toString());
  }

  /**
   * 响应失败
   *
   * @param data 需要返回的数据,需要根据实际业务指定,因此这里只能给定data对象
   * @return
   */
  public void failedFor404(Object data) throws IOException {
    result.put("code", CODE_FAILED);
    if (null != data) {
      result.put("data", data);
    } else {
      result.put("data", MSG_FAILED);
    }
    this.setStatus(RES_STATUS_404_UNKNOW_ERROR);
    this.getWriter().append(result.toString());
  }

  /**
   * 响应失败
   *
   * @param data 需要返回的数据,需要根据实际业务指定,因此这里只能给定data对象
   * @return
   */
  public void failedFor401(Object data) throws IOException {
    result.put("code", CODE_FAILED);
    if (null != data) {
      result.put("data", data);
    } else {
      result.put("data", MSG_FAILED);
    }
    this.setStatus(RES_STATUS_401_TOKEN_ERROR);
    this.getWriter().append(result.toString());
  }

  /**
   * 响应失败
   *
   * @param msg 需要返回的数据,需要根据实际业务指定,因此这里只能给定data对象
   * @return
   */
  public void failedFor301(Object msg) throws IOException {
    result.put("code", CODE_FAILED);
    if (!StringUtils.isEmpty(msg)) {
      result.put("data", msg);
    } else {
      result.put("data", MSG_FAILED);
    }
    this.setStatus(RES_STATUS_301_CODE_ERROR);
    this.getWriter().append(result.toString());
  }

  /**
   * 响应失败.
   * 前端参数传递错误!
   *
   * @param msg 需要返回的数据,需要根据实际业务指定,因此这里只能给定data对象
   * @return
   */
  public void failedFor302(String msg) throws IOException {
    result.put("code", CODE_FAILED);
    if (!StringUtils.isEmpty(msg)) {
      result.put("data", msg);
    } else {
      result.put("data", MSG_FAILED);
    }
    this.setStatus(RES_STATUS_302_PARAM_ERROR);
    this.getWriter().append(result.toString());
  }

  /**
   * 被过滤器过滤后,响应的内容!
   *
   * @param myHttpRequest
   */
  public void failedForfilter(MyHttpRequest myHttpRequest) throws IOException {
    result.put("code", CODE_FAILED);
    result.put("msg", myHttpRequest.getErrorMsg());
    this.setStatus(myHttpRequest.getStatus());
    getWriter().append(result.toString());
  }

  /**
   * 失败返回.
   * <p>
   * 响应成功,但是业务逻辑失败时,调用此接口
   *
   * @param msg 执行失败的原因.
   * @throws IOException
   */
  public void failed(String msg) throws IOException {
    result.put("code", CODE_FAILED);
    result.put("msg", msg);
    setStatus(RES_STATUS_200_SUCCESS);
    getWriter().append(result.toString());
    return;
  }

  /**
   * 获取
   */
  public Result getResult() {
    return processResult;
  }

  /**
   * 设置处理结果.
   */
  public void setResult(Result result) throws IOException {
    this.processResult = result;
    if (processResult.result) {
      success(processResult.msg);
    } else {
      failed((String) processResult.msg);
    }
  }
}
